import { $get, $post, $put, $delete } from '@js/authRequest'
import { compressLicenseAndMobile, decomporessListLicenseAndMobileAndBank } from '@src/assets/js/tool'
// 获取字典信息
export async function getInfoListByName (name) {
  const res = await $get('/api/xapc/agent/getByName', {
    name
  })
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  decomporessListLicenseAndMobileAndBank(res.data)
  return res.data
}

// 添加代理人
export async function addAgentRequest (data) {
  const license = data.license
  compressLicenseAndMobile(data)
  const res = await $post('/api/xapc/agent', data)
  data.license = license
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return {
    ...data,
    id: res.data
  }
}

// 更新代理人
export async function updateAgentRequest (data) {
  const license = data.license
  compressLicenseAndMobile(data)
  const o = JSON.parse(JSON.stringify(data))
  const res = await $put(`/api/xapc/agent/${data.id}`, o)
  data.license = license
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return data
}

// 删除代理人
export async function deleteAgentRequest (id) {
  const res = await $delete(`/api/xapc/agent/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return id
}
